package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import officeserver.log_error.UserException;
import officeserver.users.PersonalInfo;

import GUI.calendar.CalendarGridCellPanel;
import GUI.calendar.CalendarGridPanel;
import GUI.calendar.CalendarPanel;
import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.PTextField;
import GUI.helpers.ProjectConstants;
import GUI.helpers.TextFieldType;
import GUI.main.MainClient;

/**
 * @author mramsey3
 * 
 */
public class AddAppointmentDialog extends JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;
    
    private static final int FRAME_WIDTH = 475;
    private static final int FRAME_HEIGHT = 350;

    private JPanel titlePanel;

    private JPanel centerPanel;

    private JPanel firstNamePanel;
    private PTextField firstNameTextField;
    private PButton firstNameEditButton;
    private JPanel lastNamePanel;
    private PTextField lastNameTextField;
    private PButton lastNameEditButton;
    private JPanel reasonPanel;
    private PTextField reasonTextField;
    private PButton reasonEditButton;
    private JPanel doctorPanel;
    private JComboBox doctorComboBox;
    private JPanel timePanel;
    private JComboBox timeComboBox;

    private JPanel buttonPanel;
    private PButton requestButton;
    private PButton cancelButton;

    private int globalDayCount;
    private CalendarGridCellPanel cell;
    private CalendarGridPanel grid;

    /**
     * @param globalDayCount
     * @param cell
     * @param grid
     */
    public AddAppointmentDialog(int globalDayCount, CalendarGridCellPanel cell,
	    CalendarGridPanel grid) {
	this.globalDayCount = globalDayCount;
	this.cell = cell;
	this.grid = grid;
	setSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - FRAME_WIDTH) / 2;
	final int y = (screenSize.height - FRAME_HEIGHT) / 2;
	setLocation(x, y);	
	setLayout(new BorderLayout());

	add(getTitlePanel(), BorderLayout.NORTH);

	add(getCenterPanel(), BorderLayout.CENTER);
    }

    /**
     * @return the
     */
    private JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("add appointment:", 24, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(FRAME_WIDTH,
		    ProjectConstants.HEADER_HEIGHT));
	}
	return titlePanel;
    }
    
    

    /**
     * @return
     */
    private JPanel getCenterPanel() {
	if (centerPanel == null) {
	    centerPanel = new JPanel();

	    centerPanel.setLayout(new BoxLayout(centerPanel,
		    BoxLayout.PAGE_AXIS));

	    centerPanel.add(getFirstNamePanel());
	    centerPanel.add(getLastNamePanel());
	    centerPanel.add(getReasonPanel());
	    centerPanel.add(getDoctorPanel());
	    centerPanel.add(getTimePanel());
	    centerPanel.add(getButtonPanel());
	}
	return centerPanel;
    }

    /**
     * @return
     */
    private JPanel getFirstNamePanel() {
	if (firstNamePanel == null) {
	    firstNamePanel = new JPanel();
	    firstNamePanel.setLayout(new BorderLayout());
	    firstNamePanel.setBorder(new EmptyBorder(10,10,0,10));

	    JLabel label = new JLabel(ProjectConstants.CELLPAD + "first name:");
	    label.setPreferredSize(new Dimension(ProjectConstants.LABEL_WIDTH, 25));
	    firstNamePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getFirstNameTextField(), gbc1);
	    firstNamePanel.add(centerPanel, BorderLayout.CENTER);
	    
	    firstNamePanel.add(getFirstNameEditButton(),BorderLayout.EAST);
	}
	return firstNamePanel;
    }

    /**
     * @return
     */
    private PTextField getFirstNameTextField() {
	if (firstNameTextField == null) {
	    try {
		firstNameTextField = new PTextField(MainClient.getConnection()
			.getSelfPersonalInfo().getFirstName(),
			TextFieldType.LONG);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    firstNameTextField.setEditable(false);
	}
	return firstNameTextField;
    }

    /**
     * @return
     */
    private PButton getFirstNameEditButton() {
	if (firstNameEditButton == null) {
	    firstNameEditButton = new PButton("edit", ButtonType.EDIT);
	    firstNameEditButton.addActionListener(this);
	}
	return firstNameEditButton;
    }

    /**
     * @return
     */
    private JPanel getLastNamePanel() {
	if (lastNamePanel == null) {
	    lastNamePanel = new JPanel();
	    lastNamePanel.setLayout(new BorderLayout());
	    lastNamePanel.setBorder(new EmptyBorder(10,10,0,10));


	    JLabel label = new JLabel(ProjectConstants.CELLPAD + "last name:");
	    label.setPreferredSize(new Dimension(ProjectConstants.LABEL_WIDTH, 25));
	    lastNamePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getLastNameTextField(), gbc1);
	    lastNamePanel.add(centerPanel, BorderLayout.CENTER);

	    lastNamePanel.add(getLastNameEditButton(), BorderLayout.EAST);
	}
	return lastNamePanel;
    }

    /**
     * @return
     */
    private PTextField getLastNameTextField() {
	if (lastNameTextField == null) {
	    try {
		lastNameTextField = new PTextField(MainClient.getConnection()
			.getSelfPersonalInfo().getLastName(),
			TextFieldType.LONG);
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    lastNameTextField.setEditable(false);
	}
	return lastNameTextField;
    }

    /**
     * @return
     */
    private PButton getLastNameEditButton() {
	if (lastNameEditButton == null) {
	    lastNameEditButton = new PButton("edit", ButtonType.EDIT);
	    lastNameEditButton.addActionListener(this);
	}
	return lastNameEditButton;
    }

    /**
     * @return
     */
    private JPanel getReasonPanel() {
	if (reasonPanel == null) {
	    reasonPanel = new JPanel();
	    reasonPanel.setLayout(new BorderLayout());
	    reasonPanel.setBorder(new EmptyBorder(10,10,0,10));


	    JLabel label = new JLabel(ProjectConstants.CELLPAD + "reason:");
	    label.setPreferredSize(new Dimension(ProjectConstants.LABEL_WIDTH, 25));
	    reasonPanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getReasonTextField(), gbc1);
	    reasonPanel.add(centerPanel, BorderLayout.CENTER);
	    
	    reasonPanel.add(getReasonEditButton(), BorderLayout.EAST);
	}
	return reasonPanel;
    }

    /**
     * @return
     */
    private PTextField getReasonTextField() {
	if (reasonTextField == null) {
	    reasonTextField = new PTextField("enter reason", TextFieldType.LONG);
	    reasonTextField.setEditable(false);
	}
	return reasonTextField;
    }

    /**
     * @return
     */
    private PButton getReasonEditButton() {
	if (reasonEditButton == null) {
	    reasonEditButton = new PButton("edit", ButtonType.EDIT);
	    reasonEditButton.addActionListener(this);
	}
	return reasonEditButton;
    }

    /**
     * @return
     */
    private JPanel getDoctorPanel() {
	if (doctorPanel == null) {
	    doctorPanel = new JPanel();
	    doctorPanel.setLayout(new BorderLayout());
	    doctorPanel.setBorder(new EmptyBorder(10,10,0,10));


	    JLabel label = new JLabel(ProjectConstants.CELLPAD + "doctor:");
	    label.setPreferredSize(new Dimension(ProjectConstants.LABEL_WIDTH, 25));
	    doctorPanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getDoctorComboBox(), gbc1);
	    doctorPanel.add(centerPanel, BorderLayout.CENTER);
	}
	return doctorPanel;
    }

    /**
     * @return
     */
    private JComboBox getDoctorComboBox() {
	if (doctorComboBox == null) {
	    ArrayList<PersonalInfo> doctors = new ArrayList<PersonalInfo>();
	    try {
		doctors = MainClient.getConnection().getDoctors();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    String[] docs = new String[doctors.size()];
	    for (int i = 0; i < doctors.size(); i++) {
		docs[i] = doctors.get(i).getFirstName() + " "
			+ doctors.get(i).getLastName();
	    }
	    doctorComboBox = new JComboBox(docs);
	    doctorComboBox.setPreferredSize(new Dimension(250, 25));
	    doctorComboBox.setSelectedIndex(0);
	}
	return doctorComboBox;
    }

    /**
     * @return
     */
    private JPanel getTimePanel() {
	if (timePanel == null) {
	    timePanel = new JPanel();
	    timePanel.setLayout(new BorderLayout());
	    timePanel.setBorder(new EmptyBorder(10,10,0,10));


	    JLabel label = new JLabel(ProjectConstants.CELLPAD +"time:");
	    label.setPreferredSize(new Dimension(ProjectConstants.LABEL_WIDTH, 25));
	    timePanel.add(label, BorderLayout.WEST);

	    JPanel centerPanel = new JPanel();
	    centerPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.anchor = GridBagConstraints.WEST;
	    gbc1.weightx = 1;
	    centerPanel.add(getTimeComboBox(), gbc1);
	    timePanel.add(centerPanel, BorderLayout.CENTER);
	}
	return timePanel;
    }

    /**
     * @return
     */
    private JComboBox getTimeComboBox() {
	if (timeComboBox == null) {
	    boolean[][] schedule = null;
	    try {
		schedule = MainClient.getConnection().selfCheckCalendar(
			globalDayCount);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	    int[] slots = new int[schedule.length];
	    for (int i = 0; i < schedule.length; i++) {
		if (schedule[i][getDoctorComboBox().getSelectedIndex()]) {
		    slots[i] = i;
		} else {
		    slots[i] = -1;
		}
	    }
	    String[] times = new String[slots.length];
	    for (int j = 0; j < slots.length; j++) {
		if (slots[j] >= 0) {
		    times[j] = CalendarPanel.getTimeSlot(j);
		} else {
		    times[j] = "---";
		}

	    }
	    timeComboBox = new JComboBox(times);
	    timeComboBox.setPreferredSize(new Dimension(250, 25));
	}
	return timeComboBox;
    }

    /**
     * @return
     */
    private JPanel getButtonPanel() {
	if (buttonPanel == null) {
	    buttonPanel = new JPanel();
	    buttonPanel.setBorder(new EmptyBorder(20,10,10,10));

	    buttonPanel.setLayout(new GridBagLayout());

	    buttonPanel.add(getRequestButton());
	    buttonPanel.add(Box.createHorizontalStrut(10));
	    buttonPanel.add(getCancelButton());
	}
	return buttonPanel;
    }

    /**
     * @return
     */
    private PButton getRequestButton() {
	if (requestButton == null) {
	    requestButton = new PButton("Request", ButtonType.MEDIUM);
	    requestButton.addActionListener(this);
	}
	return requestButton;
    }

    /**
     * @return
     */
    private PButton getCancelButton() {
	if (cancelButton == null) {
	    cancelButton = new PButton("Cancel", ButtonType.MEDIUM);
	    cancelButton.addActionListener(this);
	}
	return cancelButton;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getFirstNameEditButton()) {
	    String newFirstName = JOptionPane.showInputDialog("First name:");
	    getFirstNameTextField().setText(newFirstName);
	} else if (e.getSource() == getLastNameEditButton()) {
	    String newLastName = JOptionPane.showInputDialog("Last name:");
	    getLastNameTextField().setText(newLastName);
	} else if (e.getSource() == getReasonEditButton()) {
	    String newReason = JOptionPane.showInputDialog("Reason:");
	    getReasonTextField().setText(newReason);
	} else if (e.getSource() == getRequestButton()) {
	    try {
		MainClient.getConnection().addSelfAppointment(
			getDoctorComboBox().getSelectedIndex(), globalDayCount,
			timeComboBox.getSelectedIndex());
	    } catch (Exception e1) {
		e1.printStackTrace();
	    }
	    setVisible(false);
	    grid.updateCell(cell);
	    cell.repaint();
	} else if (e.getSource() == getCancelButton()) {
	    setVisible(false);
	}
    }
}
